﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using ForumsBrowser.BusinessObjects;
using ForumsBrowser.ObjectGateway;
using System.Diagnostics;
using System.Windows.Controls.Primitives;

namespace ForumsBrowser
{
    /// <summary>
    /// Interaction logic for SelectedForums.xaml
    /// </summary>
    public partial class SelectedForumsView : UserControl
    {
        public event EventHandler<ViewSelectedForumsEventArgs> ViewSelected;

        public ForumList SelectedForums
        {
            get
            {
                return this.Resources["forums"] as ForumList;
            }
        }

        private List<string> _activeForums;

        public SelectedForumsView()
        {
            InitializeComponent();
            _activeForums = new List<string>();
        }

        public void AddForum(Forum f)
        {
            SelectedForums.AddUnique(f, (a, b) => a.ShortName == b.ShortName);
            
            Gateway.ForumSetSelected(f.ShortName, true);
        }

        public void RemoveForum(Forum f)
        {
            SelectedForums.Remove(f, (a, b) => a.ShortName == b.ShortName);
            Gateway.ForumSetSelected(f.ShortName, false);
        }

        protected virtual void OnViewSelected(ViewSelectedForumsEventArgs e)
        {
            EventHandler<ViewSelectedForumsEventArgs> handler = ViewSelected;
            if (handler != null)
                handler(this, e);
        }

        private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            OnViewSelected(new ViewSelectedForumsEventArgs { SelectedForums = SelectedForums });
        }

        private void Border_GotFocus(object sender, RoutedEventArgs e)
        {
            Debug.WriteLine("GotFocus");
        }

        public void ClearSelectedForums()
        {
            for (int i = SelectedForums.Count - 1; i >= 0; i--)
                RemoveForum(SelectedForums[i]);
        }

        private void CheckBox_CheckedChanged(object sender, RoutedEventArgs e)
        {
            ToggleButton button = sender as ToggleButton;
            if (button.IsChecked.Value)
            {
                this._activeForums.Add(button.Tag.ToString());
            }
            else
            {
                this._activeForums.Remove(button.Tag.ToString());
            }
            ViewSelectedForumsEventArgs args = new ViewSelectedForumsEventArgs();
            args.SelectedForums = SelectedForums.Join(this._activeForums, f => f.ShortName, sName => sName, (f, SName) => f);
            OnViewSelected(args);
        }

    }



    public class ForumList : ObservableCollection<Forum> { }
}
